package servlet;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.User;
import net.sf.json.JSONObject;

@WebServlet("/ajax")
public class AjaxServlet extends HttpServlet{

	/**
	 *  版本号是为了服务器能识别你是哪个时代的对象，从而让你下载新版本
	 */
	private static final long serialVersionUID = 2023;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String name = req.getParameter("name");
		System.out.println("name=" + name);
		
		//返回一个json对象给前端
		resp.setContentType("application/json;charset=utf-8"); 
		PrintWriter out = resp.getWriter();
		
		User user = new User();
		user.setName("蔡徐坤");
		user.setAge(26);
		user.setSex('男');
		
		//创建一个json对象
		JSONObject obj = JSONObject.fromObject(user);
		// json对象转成字符串写出去
		out.write(obj.toString());
		out.flush();
	}
}
